﻿Public Class TypologieNiv1CategForm
    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Public table1 As String = ""
    Public table2 As String = ""
    Public databaseName As String


    Sub New(ByVal databaseStr As String)
        InitializeComponent()
        databaseName = databaseStr
    End Sub

    Private Sub ActivHumTyp_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub
    Private Sub TypologieNiv1CategForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ComboBoxCodeCatg.Visible = False
        db.myReq = "SELECT * FROM  [dbo].[" & table1 & "]"
        db.Connecter(databaseName)
        Me.ComboBoxCodeCatg.Items.Clear()
        Me.ComboBoxNomCateg.Items.Clear()
        Me.ComboBoxCodeCatg.Items.Add("")
        Me.ComboBoxNomCateg.Items.Add("Tous")
        While db.rdr.Read()
            Me.ComboBoxCodeCatg.Items.Add(db.rdr(0))
            Me.ComboBoxNomCateg.Items.Add(db.rdr(1))
        End While
        db.Disconnecter()
        Me.ComboBoxCodeCatg.SelectedIndex = 0
        Entrer = ""
        DataGridViewTypol.MultiSelect = False
        DataGridViewTypol.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewTypol.ReadOnly = True
        miseajour()
    End Sub


    Private Sub miseajour()
        Dim requete As String = ""
        db.myReq = "SELECT Top 2 syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= '" & table2 & "'"
        db.Connecter(databaseName)
        While db.rdr.Read()
            If Me.ComboBoxCodeCatg.Text = "" Then
                requete = "SELECT * FROM  [dbo].[" & table2 & "] "
            Else
                requete = "SELECT * FROM  [dbo].[" & table2 & "] where [" & db.rdr(0) & "]=" & Me.ComboBoxCodeCatg.Text
            End If
        End While
        db.Disconnecter()

        db.myReq = requete
        db.Connecter(databaseName)
        DataGridViewTypol.Rows.Clear()
        While db.rdr.Read()
            DataGridViewTypol.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), db.rdr(4), db.rdr(5))
        End While
        db.Disconnecter()

        If DataGridViewTypol.RowCount - 1 = 0 Then
            Me.TextBoxCodeTypol.Text = ""
            Me.TextBoxNomTypol.Text = ""
            Me.TextBoxDef.Text = ""
        Else
            DataGridViewTypol.Rows(0).Selected = True
            Me.TextBoxCodeTypol.Text = DataGridViewTypol.SelectedCells(2).Value
            Me.TextBoxNomTypol.Text = DataGridViewTypol.SelectedCells(3).Value
            Me.TextBoxDef.Text = DataGridViewTypol.SelectedCells(4).Value
            TextBoxID = DataGridViewTypol.SelectedCells(0).Value
            Me.oidValue.Text = DataGridViewTypol.SelectedCells(5).Value
        End If
    End Sub

    Private Sub DataGridView3_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewTypol.CurrentCellChanged
        If Entrer = "debut" Then
            action = ""
            Selection()
        End If
    End Sub
    Private Sub DataGridView3_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewTypol.MouseClick
        If e.Button = MouseButtons.Left Then
            Entrer = "debut"
            action = ""
            Selection()
        End If
    End Sub
    Private Sub Selection()
        If DataGridViewTypol.RowCount - 1 = 0 Then
            Me.TextBoxCodeTypol.Text = ""
            Me.TextBoxNomTypol.Text = ""
            Me.TextBoxDef.Text = ""
        Else
            Me.TextBoxCodeTypol.Text = DataGridViewTypol.SelectedCells(2).Value
            Me.TextBoxNomTypol.Text = DataGridViewTypol.SelectedCells(3).Value
            Me.TextBoxDef.Text = DataGridViewTypol.SelectedCells(4).Value
            TextBoxID = DataGridViewTypol.SelectedCells(0).Value
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Dispose()
    End Sub

    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        action = "ajouter"
        Me.TextBoxCodeTypol.Text = ""
        Me.TextBoxNomTypol.Text = ""
        Me.TextBoxDef.Text = ""
    End Sub

    Private Sub ButtonSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSup.Click
        Try
            Dim requete As String = ""
            Entrer = ""
            action = ""
            db.myReq = "SELECT Top 1 syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= '" & table2 & "'"
            db.Connecter(databaseName)
            While db.rdr.Read()
                requete = "DELETE FROM  [dbo].[" & table2 & "] WHERE [" & db.rdr(0) & "]='" & TextBoxID & "'"
            End While
            db.Disconnecter()

            db.myReq = requete
            db.Connecter(databaseName)
            db.Disconnecter()
            miseajour()

            If DataGridViewTypol.RowCount - 1 = 0 Then
                Return
            End If
            Dim listOid As List(Of String) = New List(Of String)

            listOid.Add(DataGridViewTypol.SelectedCells(9).Value)

            _workspace.deleteNodes(_layerName, listOid)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ButtonAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.TextBoxCodeTypol.Text = ""
        Me.TextBoxNomTypol.Text = ""
        Me.TextBoxDef.Text = ""
        action = "ajouter"
    End Sub


    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Entrer = ""
        Dim requete As String = ""
        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= '" & table2 & "'"
        db.Connecter(databaseName)

        Dim tab As String() = New String(5) {}
        Dim i As Integer = 0
        While db.rdr.Read()
            tab(i) = db.rdr(0)
            i = i + 1
        End While

        If action = "ajouter" Then
            requete = "INSERT INTO  [dbo].[" & table2 & "]([" & tab(1) & "],[" & tab(2) & "],[" & tab(3) & "],[" & tab(4) & "],[SIGID])VALUES('" & Me.ComboBoxCodeCatg.Text & "','" & Me.TextBoxCodeTypol.Text & "','" & Me.TextBoxNomTypol.Text & "','" & Me.TextBoxDef.Text & "','" & Me.oidValue.Text & "')"
        Else
            requete = "Update  [dbo].[" & table2 & "] SET  [" & tab(1) & "] = '" & Me.TextBoxCodeTypol.Text & "', [" & tab(2) & "] = '" & Me.TextBoxNomTypol.Text & "',[" & tab(3) & "] = '" & Me.TextBoxDef.Text & "',[SIGID]='" & Me.oidValue.Text & "' WHERE [" & tab(0) & "]='" & TextBoxID & "'"
        End If
        MsgBox(requete)
        db.Disconnecter()


        db.myReq = requete
        db.Connecter(databaseName)
        db.Disconnecter()
        miseajour()

    End Sub

    Private Sub ComboBoxNomCatg_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomCateg.TextChanged

        If Me.ComboBoxNomCateg.FindString(Me.ComboBoxNomCateg.Text) > 0 Then
            Dim Position As Int32 = Me.ComboBoxNomCateg.Text.Length
            Me.ComboBoxNomCateg.SelectedIndex = Me.ComboBoxNomCateg.FindString(Me.ComboBoxNomCateg.Text)
            Me.ComboBoxNomCateg.SelectionStart = Position
            Me.ComboBoxNomCateg.SelectionLength = Me.ComboBoxNomCateg.Text.Length - Position
            Me.ComboBoxCodeCatg.SelectedIndex = Me.ComboBoxNomCateg.FindString(Me.ComboBoxNomCateg.Text)
        Else
            Me.ComboBoxNomCateg.Text = ""
            Me.ComboBoxCodeCatg.Text = ""
        End If
        Entrer = ""
        miseajour()
    End Sub


    ''' <summary>
    ''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private _workspace As Object
    Private _layerName As String
    Private geoForm As GeoLocation = Nothing
    Property layerName() As String
        Get
            Return _layerName
        End Get
        Set(ByVal value As String)
            _layerName = value
        End Set
    End Property

    Property workspace() As Object
        Get
            Return _workspace
        End Get
        Set(ByVal value As Object)
            _workspace = value
        End Set
    End Property

    Private Sub btnGetGeo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetGeo.Click
        If geoForm IsNot Nothing AndAlso geoForm.Visible Then
            Return
        End If

        geoForm = New GeoLocation()
        geoForm.Show()
        geoForm.workspace = Me.workspace
        geoForm.oidParentText = oidValue

    End Sub

End Class